#include "stdafx.h"
#include <XParticleEmiter.h>
#include <sstream>

XParticleEmiter::XParticleEmiter(std::string path, int time, XPosition pos)
{
	File.LoadFile(path.c_str(), 1000);
	Position = pos;
	MaxSprites = File.GetInt("MaxSprites", "[Main]");
	CreationTime = time;
	ParticleDuration = File.GetInt("ParticleDuration", "[Main]");
	DeltaUpdate = File.GetInt("DeltaUpdate", "[Main]");
	LastUpdate = time;

	Texture = new XTexture;
	Texture->Load(File.GetString("TexturePath", "[Main]"));

	LoadData();

}

void XParticleEmiter::LoadData()
{
	StartColor.R = File.GetInt("ColorR", "[Start]");
	StartColor.G = File.GetInt("ColorG", "[Start]");
	StartColor.B = File.GetInt("ColorB", "[Start]");
	StartColor.A = File.GetInt("ColorA", "[Start]");

	StartRange.X = File.GetInt("RangeX", "[Start]");
	StartRange.Y = File.GetInt("RangeY", "[Start]");

	MinStartRotation = File.GetInt("MinRotation", "[Start]");
	MaxStartRotation = File.GetInt("MaxRotation", "[Start]");

	MinStartScale = File.GetFloat("MinScale", "[Start]");
	MaxStartScale = File.GetFloat("MaxScale", "[Start]");

	ChangeColor.R = File.GetInt("ColorR", "[Change]");
	ChangeColor.G = File.GetInt("ColorG", "[Change]");
	ChangeColor.B = File.GetInt("ColorB", "[Change]");
	ChangeColor.A = File.GetInt("ColorA", "[Change]");

	Width = File.GetInt("Width", "[Change]");
	Height = File.GetInt("Height", "[Change]");

	ChangeRange.X = File.GetInt("RangeX", "[Change]");
	ChangeRange.Y = File.GetInt("RangeY", "[Change]");

	MinChangeRotation = File.GetInt("MinRotation", "[Change]");
	MaxChangeRotation = File.GetInt("MaxRotation", "[Change]");

	MinChangeScale = File.GetFloat("MinScale", "[Change]");
	MaxChangeScale = File.GetFloat("MaxScale", "[Change]");

	SpriteClip = new XRectangle[MaxSprites];

	for(int i=0; i<MaxSprites; i++)
	{
		std::ostringstream ss;
		ss << i;

		std::string str;
		str = "[";
		str += ss.str();
		str += "]";

		SpriteClip[i].X = File.GetInt("X", str);
		SpriteClip[i].Y = File.GetInt("Y", str);
		SpriteClip[i].W = File.GetInt("W", str);
		SpriteClip[i].H = File.GetInt("H", str);
	}

	Sprite = new XSprite*[MaxSprites];

	for(int i=0; i<MaxSprites; i++)
	{
		Sprite[i] = new XSprite;
	}

}

void XParticleEmiter::AddParticle(int time)
{
	int nr = rand()%MaxSprites;
	XParticle* part = new XParticle(time, ParticleDuration, Sprite[nr], Texture);

	part->SpriteClip = SpriteClip[nr];

	float scale;
	if(MaxStartScale-MinStartScale != 0 && MaxStartScale-MinStartScale>0)
		scale = (MinStartScale*100 + rand()%(int)(MaxStartScale*100-MinStartScale*100))/100;
	else
		scale = MinStartScale;

	int rotation;
	if(MaxStartRotation-MinStartRotation != 0 && MaxStartRotation-MinStartRotation>0)
		rotation = MinStartRotation*100 + rand()%(int)(MaxStartRotation*100-MinStartRotation*100)/100;
	else
		rotation = MinStartRotation;

	XVector transl;
	if(StartRange.X != 0)
		transl.X = Position.X-StartRange.X/2+rand()%(int)StartRange.X;
	else
		transl.X = Position.X;
	if(StartRange.Y != 0)
		transl.Y = Position.Y-StartRange.Y/2 + rand()%(int)StartRange.Y;
	else
		transl.Y = Position.Y;

	part->SetCurrent(StartColor, transl, rotation, scale);


	if(MaxChangeScale-MinChangeScale != 0 && MaxChangeScale-MinChangeScale>0)
		scale = (MinChangeScale*100 + rand()%(int)(MaxChangeScale*100-MinChangeScale*100))/100;
	else
		scale = MinChangeScale;

	if(MaxChangeRotation-MinChangeRotation != 0 && MaxChangeRotation-MinChangeRotation>0)
		rotation = MinChangeRotation + rand()%(MaxChangeRotation-MinChangeRotation);
	else
		rotation = MinChangeRotation;

	if(ChangeRange.X != 0)
		transl.X = Position.X+Width-ChangeRange.X/2+rand()%(int)ChangeRange.X;
	else
		transl.X = Position.X+Width;
	if(ChangeRange.Y != 0)
		transl.Y = Position.Y+Height-ChangeRange.Y/2 + rand()%(int)ChangeRange.Y;
	else
		transl.Y = Position.Y+Height;
	
	
	part->SetChange(ChangeColor, transl, rotation, scale);

	Particle.push_back(part);
		
}

void XParticleEmiter::SetPosition(XPosition pos)
{
	Position = pos;
}

void XParticleEmiter::Update(int time)
{
	int deleted = 0;
	for(int i=0; i<Particle.size()-deleted; i++)
	{
		if(Particle[i]->DurationTime <0)
		{
			deleted++;
			delete Particle[i];
			Particle.erase(Particle.begin() + i);
		}
		else
			break;
	}

	for(int i=0; i<Particle.size(); i++)
	{
		Particle[i]->Update(time);
	}

	int newParticles = 0;
	if(time - LastUpdate >= DeltaUpdate)
	{
		newParticles = ((time-LastUpdate)/DeltaUpdate);

		do
		{
			AddParticle(time);
			LastUpdate = time;
			newParticles--;

		}while(newParticles>0);
	}
}

void XParticleEmiter::Draw()
{
	for(int i=0; i<Particle.size(); i++)
	{
		Particle[i]->Draw();
	}
}